﻿using ModuleGlobalMessage.Util;
using PluginAPI.Core;
using System.Text;
using Utils.ModuleKiller;


namespace Utils.ModuleHint
{
    /// <summary>
    /// 文本助理
    /// </summary>
    public class HintUtil
    {   
        /// <summary>
        /// 获取 | Hint 文本
        /// </summary>
        public static string GetHint(Player player)
        {
            switch (player.Team)
            {
                case PlayerRoles.Team.SCPs:
                    return GetHintSCP(player);

                case PlayerRoles.Team.FoundationForces:
                    if (player.Role == PlayerRoles.RoleTypeId.FacilityGuard)
                    {
                        return GetHintGuard(player);
                    }

                    return GetHintFoundation(player);
                case PlayerRoles.Team.Scientists:
                    return GetHintScientist(player);


                case PlayerRoles.Team.ChaosInsurgency:
                    return GetHintChaos(player);
                case PlayerRoles.Team.ClassD:
                    return GetHintClassD(player);
                case PlayerRoles.Team.Dead:
                case PlayerRoles.Team.OtherAlive:
                default:
                    return GetHintObsever(player);
            }
        }


        /// <summary>
        /// 获取 | SCP 文本
        /// </summary>
        /// <param name="player"></param>
        /// <returns></returns>
        private static string GetHintSCP(Player player)
        {
            string output = $"<size=20><b>" +
                $"<align=left>{MessageBox.ToMessage()}</align>\n" +
                $"<align=right>{KillLeaderboard.Get(player).KillHuman} {KillLeaderboard.GetKillBoardSCP()}</align>\n" +
                $"\n" +
                $"<align=left>{Round.Duration:mm\\:ss}</align>\n" +
                $"<align=left>{GetPlayer(player)}</align>\n" +
                $"</b></size>\n";

            return output;
        }

        /// <summary>
        /// 获取|旁观者
        /// </summary>
        private static string GetHintObsever(Player player)
        {
            //float percentNTF = Respawning.RespawnTokensManager.GetTeamDominance(SpawnableTeamType.NineTailedFox);
            //float percentCHA = Respawning.RespawnTokensManager.GetTeamDominance(SpawnableTeamType.NineTailedFox);

            string output = $"<size=20><b>" +
                $"<align=left>{MessageBox.ToMessage()}</align>\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"<align=left>💼 门酱纯一郎</align>\n" +
                $"<align=left>{Round.Duration:mm\\:ss}</align>\n" +
                //$"<align=left>[NTF]{percentNTF} : [CHA]{percentCHA}</align>\n" +
                $"\n" +
                $"<align=left>👪 Q群960656955</align>\n" +
                $"<align=left>👪 Q群902096405（已满）</align>\n" +
                $"<align=left>👪 输入 [{".say".ToLower()} 你好] 开启聊天~";

            return output;
        }

        /// <summary>
        /// 获取|九尾狐
        /// </summary>
        /// <param name="player"></param>
        /// <returns></returns>
        private static string GetHintFoundation(Player player)
        {
            int teamMembers = Player.GetPlayers().FindAll(e => e.Team == player.Team).Count;

            string captainOnline = Player.GetPlayers().Find(e => e.Role == PlayerRoles.RoleTypeId.NtfCaptain) != null ? "<color=#01F401>在线</color>" : "<color=red>离线</color>";
            string privateOnline = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.NtfPrivate).Count.ToString();
            string leaderOnline = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.NtfSergeant).Count.ToString();
            string scientitsOnline = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.NtfSpecialist).Count.ToString();

            string output = $"<size=20><b>" +
                $"<align=left>{MessageBox.ToMessage()}</align>\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"<align=left>{Round.Duration:mm\\:ss}</align>\n" +
                $"<align=left>💼 肃清收容站点威胁</align>\n" +            
                $"<align=left><color=#0378FF>👲</color> {captainOnline}</align>\n" +
                $"<align=left><color=#03BBFF>👲</color> {leaderOnline}</align>\n" +
                $"<align=left><color=#B7D9FF>👲</color> {privateOnline}</align>\n" +
                $"<align=left><color=#03FFE5>👲</color> {scientitsOnline}</align>\n";



            return output;
        }

        /// <summary>
        /// 获取|科学家
        /// </summary>
        private static string GetHintScientist(Player player)
        {
            string scientistOnline = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.Scientist).Count.ToString();
            string facilityGuardOnline = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.FacilityGuard).Count.ToString();

            string output = $"<size=20><b>" +
                $"<align=left>{MessageBox.ToMessage()}</align>\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"<align=left>{Round.Duration:mm\\:ss}</align>\n" +
                $"<align=left>💼 撤离收容站点</align>\n" +
                $"<align=left><color=#FEE649>👲</color> {scientistOnline}</align>\n" +
                $"<align=left><color=#C5C5C4>👲</color> {facilityGuardOnline}</align>\n";
             

            return output;
        }


        private static string GetHintGuard(Player player)
        {
            string scientistOnline = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.Scientist).Count.ToString();
            string facilityGuardOnline = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.FacilityGuard).Count.ToString();

            string output = $"<size=20><b>" +
                $"<align=left>{MessageBox.ToMessage()}</align>\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
               $"<align=left>{Round.Duration:mm\\:ss}</align>\n" +
                $"<align=left>💼 撤离<color=#FEE649>科学家</color></align>\n" +
                $"<align=left><color=#FEE649>👲</color> {scientistOnline}</align>\n" +
                $"<align=left><color=#C5C5C4>👲</color> {facilityGuardOnline}</align>\n";

            return output;
        }

        private static string GetHintClassD(Player player)
        {
            string online = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.ClassD).Count.ToString();
            string chaosOnline = Player.GetPlayers().FindAll(e => e.Team == PlayerRoles.Team.ChaosInsurgency).Count.ToString();

            string output = $"<size=20><b>" +
                $"<align=left>{MessageBox.ToMessage()}</align>\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"<align=left>{Round.Duration:mm\\:ss}</align>\n" +
                $"<align=left>💼 逃离收容站点</align>\n" +
                $"<align=left><color=#F8A201>👲</color> {online}</align>\n" +
                $"<align=left><color=#31CE01>👲</color> {chaosOnline}</align>\n";

            return output;
        }

        private static string GetHintChaos(Player player)
        {
            string classDOnline = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.ClassD).Count.ToString();
            string chaosOnline = Player.GetPlayers().FindAll(e => e.Team == PlayerRoles.Team.ChaosInsurgency).Count.ToString();

            string output = $"<size=20><b>" +
                $"<align=left>{MessageBox.ToMessage()}</align>\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"\n" +
                $"<align=left>{Round.Duration:mm\\:ss}</align>\n" +
                $"<align=left>💼 占领收容站点</align>\n" +
                $"<align=left><color=#31CE01>👲</color> {chaosOnline}</align>\n" +
                $"<align=left><color=#F8A201>👲</color> {classDOnline}</align>\n";

            return output;
        }

        private static string GetPlayer(Player player)
        {
            var matches = Player.GetPlayers().FindAll(p => p.Team == player.Team);

            //matches.RemoveAll(e => e.Role == PlayerRoles.RoleTypeId.Scp0492);
            matches.Sort((a, b) => a.Health.CompareTo(b.Health));

            StringBuilder build = new StringBuilder();
            {
                build.AppendLine("警戒生命线");
                build.AppendLine("--------");
            }


            int maxShow = 4;

            if (matches.Count >= maxShow)
            {

                for (int i = 0; i < 4; i++)
                {
                    string message = string.Empty;

                    if (matches[i] == player)
                    {
                        message = $"<color=#8EE50C>{matches[i].Role.ToString().ToUpper().PadRight(8)}</color>💓 {matches[i].Health:F0}";
                    }
                    else
                    {
                        message = $"<align=left><color=red>{matches[i].Role.ToString().ToUpper().PadRight(8)}</color>💓 {matches[i].Health:F0}";
                    }

                    build.AppendLine(message);
                }

                return build.ToString();
            }


            for (int i = 0; i < matches.Count; i++)
            {
                string message = string.Empty;

                if (matches[i] == player)
                {
                    message = $"<color=#8EE50C>{matches[i].Role.ToString().ToUpper().PadRight(8)}</color>💓 {matches[i].Health}";
                    }
                else
                {
                    message = $"<color=red>{matches[i].Role.ToString().ToUpper().PadRight(8)}</color>💓 {matches[i].Health}";
                }

                build.AppendLine(message);
            }

            for (int j = matches.Count; j < maxShow; j++)
            {
                build.AppendLine("");
            }


            return build.ToString();
        }
    }
}
